import Common from '../../common/common.js';
import BaseWin from '../base/BaseWin.js';
import TimNet from "../../timnet.js";
import Talk from "./Talk.js";
/***
 * 信息操作
 ***/
function MessWin (e,item) {
    //人员列表对象
    let event = e || window.event;
    this.pageX = event.pageX;
    this.pageY = event.pageY;
    this.item = item;
    BaseWin.call(this);
    this.element.className += " messWin";
}
let prototype = Object.create(BaseWin.prototype);
prototype.constructor = MessWin;
MessWin.prototype  = prototype;

MessWin.prototype.initElement = function(){

    let menus = [];
    switch (this.item.data.type) {
        case TIM.TYPES.MSG_FILE:
            menus.push({
                label: "li",
                tag:"download",
                props: {className:"download",innerText: "下载"}
            })
            break;
        case TIM.TYPES.MSG_IMAGE:
            menus.push({
                label: "li",
                tag:"lookImage",
                props: {className:"lookImage",innerText: "查看"}
            })
            break;
        default :
            break;
    }

    if (this.item.data.flow == "out") {
        menus.push({
            label: "li",
            tag:"revoke",
            props: {className:"revoke",innerText: "撤回"}
        });
    }
    if(menus.length == 0){
        return;
    }

    const ul = Common.tool.createElement({
        label: "ul",
        props: {className: "messControls", style: "left:" + this.pageX + "px;top:" + this.pageY + "px"},
        childNodes: menus
    });
    this.element = ul;
    window.messControls = this;

    const close =()=>{
        window.messControls = false;
        this.cancel();
    }
    //查看
    if(ul.lookImage){
        Common.tool.addHandle(ul.lookImage,"click",()=>{
             new ImageWin(this.item.data.payload.imageInfoArray.filter(info=>info.type==0)[0].imageUrl).create();
            close();
        });
    }

    //下载
    if(ul.download){
        Common.tool.addHandle(ul.download,"click",()=>{
            window.open(this.item.data.payload.fileUrl);
            close();
        });
    }
    //撤回
    if(ul.revoke){
        Common.tool.addHandle(ul.revoke,"click",()=>{
            TimNet.revokeMessage(this.item.data,(e)=>{
                close();
                this.item.remove();
                let nick = Common.cache.myProfile.nick;
                let userID = Common.cache.myProfile.userID;
                Talk.sendInfoMess("\""+(Common.tool.isNull(nick)?userID:nick)+"\"撤回一条消息");
            },(e)=>{
                Talk.sendErrorMess(e);
                close();
            })
        });
    }

}



function ImageWin (url) {
    //人员列表对象
    this.url = url;
    BaseWin.call(this);
    this.element.className += " lookImageWin";
}

let prototype2 = Object.create(BaseWin.prototype);
prototype2.constructor = ImageWin;
ImageWin.prototype  = prototype2;
ImageWin.prototype.initBox = function(){
    let image = new Image();
    image.src = this.url;
    image.style.maxWidth = "1200px";
    image.style.maxHeight = "800px";
    this.element.win.box.appendChild(image);
 }


export {MessWin} ;
